<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\Business;

use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;

/**
 * 商家DAO
 *
 * @author 李想(928674263@qq.com)
 * @package App\Dao
 */
class BusinessDao extends Dao
{
    use DaoTrait;

    public function model()
    {
        return Business::class;
    }

    /**
     * 查询商家
     *
     * @param array $map
     * @return mixed
     */
    public function first(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
            return queryWhere($query, $map);
            })
            ->first();
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getBusinessList(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->orderByDesc('sort')
            ->orderBy('id')
            ->select('id', 'name', 'logo', 'tag', 'province', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate', 'enable_full_discount', 'enable_coupon', 'enable_voucher')
            ->get();
    }

    /**
     * 获取商家列表
     *
     * @param array $map 查询条件
     * @return mixed
     */
    public function getBusinessFirst(array $map = [])
    {
        return Business::query()
            ->where(function(Builder $query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->orderByDesc('sort')
            ->orderBy('id')
            ->select('id', 'name', 'science', 'description','enable_coupon', 'enable_voucher', 'enable_full_discount' ,'forenoon_hours', 'afternoon_hours', 'hours_describe','logo', 'business_cover', 'province', 'city', 'district', 'address', 'longitude', 'latitude', 'tel', 'full_discount_rate')
            ->first();
    }

    /**
     * 通过商品ID判断商品是否存在
     *
     * @author 张昊(2396675557@qq.com)
     *
     * @param int $id
     * @return bool
     */
    public function existsBusinessById(int $id)
    {
        if ($id === 0) return false;

        return Business::query()->where('id', $id)->exists();
    }
}